package cn.fl.insurance.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.fl.insurance.vo.InsCompInfoVO;
import cn.fl.insurance.vo.InsFeeDeductRecVO;
import cn.fl.insurance.vo.InsTypCfgRVO;
import cn.fl.preloan.condition.InsTypInfoQueryCondition;
import cn.fl.preloan.dto.*;
import cn.fl.preloan.enums.PreloanResultCodeEnum;
import cn.fl.preloan.insurance.IInsTypInfosService;
import com.alibaba.dubbo.config.annotation.Reference;
import cn.fl.commons.result.ResponseData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.insurance.constant.InsuranceConstant;
import cn.fl.insurance.service.IInsTypInfoService;
import cn.fl.insurance.vo.InsTypInfoVO;
import cn.fl.system.constant.SysLogConstant;
import net.sf.json.JSONArray;

/**
 * @Description: 保险种类基础表 控制层
 */
@Controller
@RequestMapping("/insurance/insTypInfo")
public class InsTypInfoController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(InsTypInfoController.class);

    /**
     * InsTypInfo 服务层BEAN
     */
    @Autowired
    private IInsTypInfoService insTypInfoService;

    /**
     * 数据字典 服务层BEAN
     */
    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService;
    @Autowired
    private IInsTypInfosService insTypInfosService;

    /**
     * 到列表页面
     */
    @RequestMapping(value = "/toListPage.do", method = {RequestMethod.GET})
    public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
        BaseLogUtils.info(logger, "toListPage", "加载InsTypInfo列表页面.", CurrentThreadContext.getCurrentUserName());
        ModelAndView mav = new ModelAndView("insurance/insTypInfo/insTypInfoList");
        this.prepare(request, mav);
        return mav;
    }

    /**
     * 到险种管理页面
     */
    @RequestMapping(value = "/toMgrPage.do", method = {RequestMethod.GET})
    public ModelAndView toMgrPage(HttpServletRequest request, HttpServletResponse response) {
        BaseLogUtils.info(logger, "toListPage", "加载到险种管理页面.", CurrentThreadContext.getCurrentUserName());
        ModelAndView mav = new ModelAndView("insurance/insTypInfo/insTypInfoMgr");
        this.prepare(request, mav);
        return mav;
    }


    /**
     * 到新增页面
     */
    @RequestMapping(value = "/toAddPage.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView toAddPage(HttpServletRequest request, HttpServletResponse response) {
        BaseLogUtils.info(logger, "toAddPage", "加载InsTypInfo Add页面.", CurrentThreadContext.getCurrentUserName());
        ModelAndView mav = new ModelAndView("insurance/insTypInfo/insTypInfoModify");
        this.prepare(request, mav);
        return mav;
    }

    /**
     * 到修改页面
     */
    @RequestMapping(value = "/toModifyPage.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView toModifyPage(HttpServletRequest request, HttpServletResponse response) {
        BaseLogUtils.info(logger, "toModifyPage", "加载InsTypInfo Modify页面.", CurrentThreadContext.getCurrentUserName());
        ModelAndView mav = new ModelAndView("insurance/insTypInfo/insTypInfoModify");
        this.prepare(request, mav);

        String id = request.getParameter("id");
        GetByIdRequest req = new GetByIdRequest();
        req.setId(Long.valueOf(id));
        ResponseData data = this.insTypInfosService.selectInsTypInfoById(req);
        InsTypInfoVO insTypInfoVO = new InsTypInfoVO();
        BeanUtils.copyProperties(data.getData(), insTypInfoVO);
        mav.addObject("insTypVO", insTypInfoVO);
        return mav;
    }

    public void prepare(HttpServletRequest request, ModelAndView mav) {
        //加载数据字典数据、传值等 ？
        List<KeyValueVO> insTypList=this.sysDictionaryApiService.getKvList(InsuranceConstant.CODE_INS_TYP_CD,CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
        /*InsTypInfoQueryCondition condition = new InsTypInfoQueryCondition();
        condition.setTypLvl(InsuranceConstant.CODE_INS_TYP_LVL_CD);//保险大类
        List<KeyValueVO> insTypList = this.insTypInfoService.searchInsTypCdList(condition);*/
        List<KeyValueVO> typClsList = this.sysDictionaryApiService.getKvList(InsuranceConstant.CODE_INS_CLS_CD, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
        mav.addObject("typClsCdList", JSON.toJSONString(typClsList));//险种类型
        mav.addObject("insTypCdList", JSON.toJSONString(insTypList));//保险种类
    }

    /**
     * 根据保险类型查询险种列表
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/getInsTypInfoList.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object getInsTypInfoList(HttpServletRequest request, HttpServletResponse response) {
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "查询成功");
        InsTypInfoQueryCondition qc = new InsTypInfoQueryCondition();
        String insTypCd = request.getParameter("insTypCd");
        if (StringUtils.isNotEmpty(insTypCd)) {
            //qc.setInsTypCd(insTypCd.trim());
            qc.setTypPid(Long.valueOf(insTypCd));//保险父类
        } else {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("保险类型不能为空");
            BaseLogUtils.info(logger, "removeInsTypInfoByIds", "根据保险父类查询险种列表数据,保险种类为空!", CurrentThreadContext.getCurrentUserName());
            return result;
        }
        GetInsTypeInfoListRequest req = new GetInsTypeInfoListRequest();
        req.setCondition(qc);
        BaseLogUtils.info(logger, "getInsTypInfoList", "根据保险父类查询险种列表数据.DATA:" + insTypCd, CurrentThreadContext.getCurrentUserName());
        ResponseData data = insTypInfosService.selectListByCondition(req);
        result.setData(data.getData());
        return result;
    }

    /**
     * 初始化险种列表
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/initList.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object initInsTypInfoList(HttpServletRequest request, HttpServletResponse response) {
        String currentPage = request.getParameter("page");
        String pageSize = request.getParameter("pagesize");
        Pager page = new Pager();
        if (StringUtils.isNotEmpty(currentPage)) {
            page.setCurrentPage(Integer.parseInt(currentPage));
        }

        if (StringUtils.isNotEmpty(pageSize)) {
            page.setEveryPage(Integer.parseInt(pageSize));
        }

        InsTypInfoQueryCondition qc = new InsTypInfoQueryCondition();
        //设置查询条件？
        String typNm = request.getParameter("typNm");//保险名称
        if (StringUtils.isNotEmpty(typNm)) {
            qc.setTypNm(typNm.trim());
        }
        String typClsCd = request.getParameter("typClsCd");//保险类型
        if (StringUtils.isNotEmpty(typClsCd)) {
            qc.setTypClsCd(typClsCd.trim());
        }
        String typPid = request.getParameter("typPid");//保险大类
        if (StringUtils.isNotEmpty(typPid)) {
            qc.setTypPid(Long.valueOf(typPid));
        }
        page.setCondition(qc);

        BaseLogUtils.info(logger, "initInsTypInfoList", "初始化InsTypInfo列表页面数据.DATA:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
        List<InsTypInfoVO> insTypInfoVOList = new ArrayList<>();
        ResponseData<InsTypInfoPageResponse> data = insTypInfosService.searchListPageByCondition(page.getCurrentPage(), page.getEveryPage(), qc);
        List<InsTypInfoDTO> insTypInfoDTOSList = data.getData().getInsTypInfoDTOSList();
        for(InsTypInfoDTO insTypInfoDTO :insTypInfoDTOSList){
            InsTypInfoVO ins = new InsTypInfoVO();
            BeanUtils.copyProperties(insTypInfoDTO, ins);
            insTypInfoVOList.add(ins);
        }
        ListPageVO<InsTypInfoVO> listPage = new ListPageVO<InsTypInfoVO>(insTypInfoVOList, data.getData().getTotalCount());
        return listPage;
    }

    /**
     * 初始化险种列表
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/selectTypList.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object selectTypList(HttpServletRequest request, HttpServletResponse response) {
        InsTypInfoQueryCondition qc = new InsTypInfoQueryCondition();
        //设置查询条件？
        String typNm = request.getParameter("typNm");//保险名称
        if (StringUtils.isNotEmpty(typNm)) {
            qc.setTypNm(typNm.trim());
        }
        String typClsCd = request.getParameter("typClsCd");//保险类型
        if (StringUtils.isNotEmpty(typClsCd)) {
            qc.setTypClsCd(typClsCd.trim());
        }
        String typPid = request.getParameter("typPid");//保险大类
        if (StringUtils.isNotEmpty(typPid)) {
            qc.setTypPid(Long.valueOf(typPid));
        }
        qc.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
        qc.setSysCd(CurrentThreadContext.getCurrentSysCd());
        BaseLogUtils.info(logger, "initInsTypInfoList", "初始化InsTypInfo列表页面数据.", CurrentThreadContext.getCurrentUserName());
        GetInsTypeInfoListRequest req = new GetInsTypeInfoListRequest();
        req.setCondition(qc);
        List<InsTypInfoVO> insTypInfoVOList = new ArrayList<>();
        ResponseData<InsTypInfoPageResponse> data = this.insTypInfosService.selectListByCondition(req);
        List<InsTypInfoDTO> insTypInfoDTOSList = data.getData().getInsTypInfoDTOSList();
        for(InsTypInfoDTO insTypInfoDTO :insTypInfoDTOSList){
            InsTypInfoVO ins = new InsTypInfoVO();
            BeanUtils.copyProperties(insTypInfoDTO, ins);
            insTypInfoVOList.add(ins);
        }
        InsTypInfoVO insTypInfoVO = new InsTypInfoVO();
        BeanUtils.copyProperties(data.getData(), insTypInfoVO);
        ListPageVO<InsTypInfoVO> listPage = null;
        if(CollectionUtils.isNotEmpty(insTypInfoVOList)){
            listPage = new ListPageVO<InsTypInfoVO>(insTypInfoVOList, insTypInfoVOList.size());
        }else{
            listPage = new ListPageVO<InsTypInfoVO>(null, 0);
        }
        return listPage;
    }

    /**
     * 删除数据
     */
    @RequestMapping(value = "/removeByIds.do", method = {RequestMethod.POST})
    @ResponseBody
    public DataResultVO removeInsTypInfoByIds(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        String ids = request.getParameter("ids");
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_DELETE_SUCC);
        if (StringUtils.isEmpty(ids)) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
            BaseLogUtils.info(logger, "removeInsTypInfoByIds", "删除InsTypInfo数据,主键参数为空!", CurrentThreadContext.getCurrentUserName());
            return result;
        }

        List<Long> idsList = new ArrayList<Long>();
        JSONArray idsArray = JSONArray.fromObject(ids);
        for (Object object : idsArray) {
            idsList.add(Long.valueOf(object.toString()));
        }

        try {
            DeleteByIdsRequest req = new DeleteByIdsRequest();
            req.setIds(idsList);
            ResponseData data = this.insTypInfosService.deleteByIds(req);
            if(!data.getCode().equals(PreloanResultCodeEnum.SUCCESS.getCode())){
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo(SysLogConstant.LOG_CMM_DELETE_FAIL);
                BaseLogUtils.info(logger, "removeInsTypInfoByIds", "删除InsTypInfo数据失败,ID:" + idsList.toString(), CurrentThreadContext.getCurrentUserName());
            }
            BaseLogUtils.info(logger, "removeInsTypInfoByIds", "删除InsTypInfo数据成功,ID:" + idsList.toString(), CurrentThreadContext.getCurrentUserName());
        } catch (BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
            BaseLogUtils.info(logger, "removeInsTypInfoByIds", "删除InsTypInfo数据失败,ID:" + idsList.toString() + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        } catch (Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
            BaseLogUtils.error(logger, "removeInsTypInfoByIds", "删除InsTypInfo数据失败,系统错误.ID:" + idsList.toString() + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        }
        return result;
    }

    /**
     * 得到一条记录
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getById.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object getInsTypInfoById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        String id = request.getParameter("id");
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
        if (StringUtils.isEmpty(id)) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
            BaseLogUtils.info(logger, "getInsTypInfoById", "获取InsTypInfo数据,主键参数为空.", CurrentThreadContext.getCurrentUserName());
            return result;
        }

        try {
            GetByIdRequest req= new GetByIdRequest();
            req.setId(Long.valueOf(id));
            ResponseData data = this.insTypInfosService.selectInsTypInfoById(req);
            InsFeeDeductRecVO insFeeDeducRecVO = new InsFeeDeductRecVO();
            BeanUtils.copyProperties(data.getData(), insFeeDeducRecVO);
            if (null == insFeeDeducRecVO) {
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo(SysLogConstant.LOG_CMM_QUERY_DATA_IS_EMPTY);
                BaseLogUtils.info(logger, "getInsTypInfoById", "获取InsTypInfo数据不存在,ID:" + id, CurrentThreadContext.getCurrentUserName());
            }
            result.setData(insFeeDeducRecVO);
            BaseLogUtils.info(logger, "getInsTypInfoById", "获取InsTypInfo数据成功,ID:" + id, CurrentThreadContext.getCurrentUserName());
        } catch (BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
            BaseLogUtils.info(logger, "getInsTypInfoById", "获取InsTypInfo数据失败,ID:" + id + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        } catch (Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
            BaseLogUtils.error(logger, "getInsTypInfoById", "获取InsTypInfo数据失败,系统错误.ID:" + id + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        }
        return result;
    }

    /**
     * 新增
     */
    @RequestMapping(value = "/add.do", method = {RequestMethod.POST})
    @ResponseBody
    public Object addInsTypInfo(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
        InsTypInfoVO entity = null;//获取参数
        try {
            entity = this.getRequestParamForInsTypInfo(request);//获取参数
            AddInsTypInfoRequest req = new AddInsTypInfoRequest();
            InsTypInfoDTO dto = new InsTypInfoDTO();
            BeanUtils.copyProperties(entity, dto);
            req.setDto(dto);
            ResponseData data  = this.insTypInfosService.addInsTypInfo(req);
            result.setData(dto.getId());
            BaseLogUtils.info(logger, "addInsTypInfo", "新增InsTypInfo数据成功,ID:" + dto.getId() + ".DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
        } catch (BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR + ex.getMessage());
            BaseLogUtils.info(logger, "addInsTypInfo", "新增InsTypInfo数据失败.DATA:" + JSONObject.toJSONString(entity) + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        } catch (Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR + ex.getMessage());
            BaseLogUtils.error(logger, "addInsTypInfo", "新增InsTypInfo数据失败,系统错误.DATA:" + JSONObject.toJSONString(entity) + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        }
        return result;
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/modify.do", method = {RequestMethod.POST})
    @ResponseBody
    public Object modifyInsTypInfo(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_UPDATE_SUCC);
        InsTypInfoVO entity = null;
        try {
            entity = this.getRequestParamForInsTypInfo(request);
            if (entity.getId() == null) {
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
                BaseLogUtils.info(logger, "modifyInsTypInfo", "修改InsTypInfo信息,主键参数为空！.ID:" + entity.getId(), CurrentThreadContext.getCurrentUserName());
                return result;
            }
            ModifyInsTypInfoRequest req = new ModifyInsTypInfoRequest();
            InsTypInfoDTO dto = new InsTypInfoDTO();
            BeanUtils.copyProperties(entity, dto);
            req.setDto(dto);
            ResponseData data  = this.insTypInfosService.modifyInsTypInfo(req);
            if (!data.getCode().equals(PreloanResultCodeEnum.SUCCESS.getCode())) {
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo(SysLogConstant.LOG_CMM_UPDATE_FAIL);
                BaseLogUtils.info(logger, "modifyInsTypInfo", "修改InsTypInfo数据失败.DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
                return result;
            }
            BaseLogUtils.info(logger, "modifyInsTypInfo", "修改InsTypInfo数据成功.DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
        } catch (BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR + ex.getMessage());
            BaseLogUtils.info(logger, "modifyInsTypInfo", "修改InsTypInfo数据失败.DATA:" + JSONObject.toJSONString(entity) + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        } catch (Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR + ex.getMessage());
            BaseLogUtils.error(logger, "modifyInsTypInfo", "修改InsTypInfo数据失败,系统错误.DATA:" + JSONObject.toJSONString(entity) + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        }
        return result;
    }

    /**
     * 获取请求参数
     *
     * @param request 请求
     * @return
     */
    private InsTypInfoVO getRequestParamForInsTypInfo(HttpServletRequest request) throws BaseException {
        //获取参数
        String id = request.getParameter("id");//主键
        String typNm = request.getParameter("typNm");//险种名称
        String typClsCd = request.getParameter("typClsCd");//险种类型 :  1必保险种、0可选险种
        String insTypCd = request.getParameter("insTypCd");//保险种类：商业险 、交强险、财产险、承运险
        String typNo = request.getParameter("typNo");//险种标示
        String typRate = request.getParameter("typRate");//费率
        String typRem = request.getParameter("typRem");//备注
        String isDefVi = request.getParameter("isDefVi");//是否默认显示
        String typPid = request.getParameter("typPid");//险种父类

        InsTypInfoVO entity = new InsTypInfoVO();
        //验证参数
        if (StringUtils.isBlank(typNm)) {
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_604, "险种名称不能为空.");
        }
        if (StringUtils.isBlank(typClsCd)) {
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_604, "险种类型不能为空.");
        }
        /*if (StringUtils.isBlank(insTypCd)) {
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_604, "保险种类不能为空.");
        }*/
        if (StringUtils.isNotBlank(typPid) && !"0".equals(typPid)) {
            entity.setTypPid(Long.valueOf(typPid));
            entity.setTypLvl(1);//子险种
        }
        if (StringUtils.isNotBlank(typRate)) {
            if (!StringUtils.isUnsignedNumeric(typRate)) {
                throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_604, "请填写正确的险种费率.");
            }
            entity.setTypRate(Double.valueOf(typRate));
        }

        entity.setTypNm(typNm);
        entity.setTypNo(typNo);
        //entity.setInsTypCd(insTypCd);
        entity.setTypClsCd(typClsCd);
        entity.setTypRem(typRem);
        if (StringUtils.isNotBlank(isDefVi)) {
            entity.setIsDefVi(Integer.valueOf(isDefVi));
        } else {
            entity.setIsDefVi(0);
        }

        Date tm = new Date();
        if (StringUtils.isNotBlank(id)) {
            entity.setId(Long.valueOf(id));
            entity.setMdfTm(tm);
            entity.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
        } else {
            entity.setCrtTm(tm);
            entity.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
        }
        return entity;
    }
}

